<?php

/**
 * 
 * @author yuxun
 *
 */
class ReplyTemplateService extends Service{
	
	public static function indexHeader(){
		return array(
				'id'         => array('text' => 'ID', 'rule' => 'int'),
				'name'       => array('text' => '名称'),
				'title'          => array('text' => '邮件标题'),
				'content'        => array('text' => '邮件内容','width' => '350px'),
				'item'           => array('text' => '道具'),
				'create_time'     => array('text' => '添加时间'),
				'action'     => array('text' => '操作')
		);
	}
	
	public static function indexData($params){
		$user = AccAuth::me();

		$modReplyTemplate = new ModReplyTemplate();
		$result = $modReplyTemplate->select($params);
	
		foreach($result as &$v){
			$v['content'] = '<div style="white-space:normal;">'.$v['content'].'</div>';
			
			$item_array = json_decode($v['item'],true);
			$item = '';
			foreach ($item_array as $key=>$val){
				if($val['is_bind'] == 1){
					$bind = '绑定';
				}
				if($val['is_bind'] == 0){
					$bind = '不绑定';
				}
				$item .= $val['item_name'].'-'.$val['item_id'].'-'.$val['item_number'].'-'.$bind;
				$item .= '</br>';
			}
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			$v['item'] = $item;

            $v['action'] .= ' <a href="'.PubFunc::buildUrl('/oss/?r=replyTemplate/edit').'&id='.$v['id'].'">编辑</a>';
			$v['action'] .= ' <a href="'.PubFunc::buildUrl('/oss/?r=replyTemplate/delete').'&id='.$v['id'].'" confirm="是否要删除该条服信息?">删除</a>';
		}
		unset($v);
	
		return $result;
	}
	
	
	
	public static function doAdd($params){
		if(PubFunc::checkEmpty($params, array('title','content'))){
			return array('state' => false, 'msg' => '缺少必要参数.');
		}

		$user = AccAuth::me();
		$modReplyTemplate = new ModReplyTemplate();
				
		$item = '';
		if($params['is_send_good'] == 2){ //不发送道具
			$item = '';
		}
				
		if($params['is_send_good'] == 1){ //发送道具
			$person_item = array();
			foreach ($params['item_id'] as $key=>$val){
				$data = array();
				$data['item_name'] = $params['item_name'][$key];
				$data['item_id'] = $params['item_id'][$key];
				$data['item_number'] = $params['item_number'][$key];
				$data['is_bind'] = $params['is_bind'][$key];
				array_push($person_item, $data);
			}
			$item = json_encode($person_item);
		}
	
		$modReplyTemplate->game          = $params['g'];
		$modReplyTemplate->name       = $params['name'];
		$modReplyTemplate->title       = $params['title'];
		$modReplyTemplate->content       = $params['content'];
		$modReplyTemplate->item = $item;
		$modReplyTemplate->create_user   = $user['UserID'];
		$modReplyTemplate->create_time   = time();
		$modReplyTemplate->update_user   = $user['UserID'];
		$modReplyTemplate->update_time   = time();

		$result = $modReplyTemplate->save();
		return array('state' => true, 'msg' =>'添加成功');	
	}
	
	public static function get($id){
		$modReplyTemplate = new ModReplyTemplate();
		$obj = $modReplyTemplate->findByPk($id);
	
		if($obj){
			$info = $obj->getAttributes();
			if($info['item'] != ''){
				$info['item'] = json_decode($info['item'],true);
			}
			
			return $info;
		}else{
			return false;
		}
	}
	
	public static function doEdit($id, $params){
		if(empty($id)) return array('state' => false, 'msg' => '信息不存在');
	
		$user = AccAuth::me();
		$modReplyTemplate = new ModReplyTemplate();
		
		$item = '';
		if($params['is_send_good'] == 2){ //不发送道具
			$item = '';
		}
		
		if($params['is_send_good'] == 1){ //发送道具
			$person_item = array();
			foreach ($params['item_id'] as $key=>$val){
				$data = array();
				$data['item_name'] = $params['item_name'][$key];
				$data['item_id'] = $params['item_id'][$key];
				$data['item_number'] = $params['item_number'][$key];
				$data['is_bind'] = $params['is_bind'][$key];
				array_push($person_item, $data);
			}
			$item = json_encode($person_item);
		}
		
		$update = array();
		$update['name'] = $params['name'];
		$update['title'] = $params['title'];
		$update['content'] = $params['content'];
		$update['item'] = $item;
		$update['update_user']  = $user['UserID'];
		$update['update_time']  = time();
			
		$result = $modReplyTemplate->updateByPk($id, $update);
	
		return array('state' => true, 'msg' =>'编辑成功');
	}
	
	public static function delete($id){
		if(empty($id)) return false;
	
		$modReplyTemplate = new ModReplyTemplate();
		$modReplyTemplate->deleteByPk($id);
		return true;
	}
	
	
	public static function getList(){
		$modReplyTemplate = new ModReplyTemplate();
		$game = PubBase::getCurrentGame();
		$params['g'] = $game;
		$list = $modReplyTemplate->select($params);
		
		$data = array();
		foreach ($list as $key=>$val){
			$data[$val['id']] = $val['name'];
		}

		return $data;
	}

	public static function getInfo($id){
		$info = self::get($id);
		if($info !== false){
			if($info['item'] == ''){
				$item = '<font color="red">未设置物品</font>';
			}else{
				$item = '';
				foreach ($info['item'] as $key=>$val){
					if($val['is_bind'] == 1){
						$bind = '绑定';
					}
					if($val['is_bind'] == 0){
						$bind = '不绑定';
					}
					$item .= $val['item_name'].'-'.$val['item_id'].'-'.$val['item_number'].'-'.$bind;
					$item .= '</br>';
				}
			}
			 
			$info['item'] = $item;
			$out = array('status'=>1,'msg'=>'查询成功','data'=>$info);
		}else{
			$out = array('status'=>0,'msg'=>'找不到该信息','data'=>'');
		}
		
		return $out;
	}
	

}